home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / segal / common.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-24  |  13.4 KB  |  549 lines

  1. /*
  2.  *    common.h - to be included with all SEGAL source files.
  3.  *
  4.  *    By Bryan Skene
  5.  *
  6.  */
  7.  
  8. /***** Include Files *****/
  9. #include <stdio.h>
  10. #include <memory.h>
  11. #include <math.h>
  12. #include <sys/param.h>
  13. #include <sys/types.h>
  14. #include <varargs.h>
  15.  
  16. /* XView includes */
  17. #include <xview/xview.h>
  18. #include <xview/panel.h>
  19. #include <xview/textsw.h>
  20. #include <xview/xv_xrect.h>
  21. #include <xview/cms.h>
  22. #include <xview/cursor.h>
  23. #include <xview/scrollbar.h>
  24. #include <xview/svrimage.h>
  25. #include <xview/notice.h>
  26. #include <xview/notify.h>
  27.  
  28. /* X Windows includes */
  29. #include <X11/Xlib.h>
  30. #include <X11/Xutil.h>
  31. #include <X11/cursorfont.h>
  32.   
  33. /* HIPS include file */
  34. #include "hipl_format.h"
  35.  
  36. #include "segal.h"
  37. #define X_WINDOW_DEP
  38. #include "function.h"
  39.  
  40. /***** Macros *****/
  41. #define Calloc(x,y) (y *)calloc((unsigned)(x), sizeof(y))
  42. #define Fread(a,b,c,d) fread((char *)(a), b, (int)(c), d)
  43. #define Fwrite(a,b,c,d) fwrite((char *)(a), b, (int)(c), d)
  44.  
  45. /* MONO returns total intensity of r,g,b components */
  46. #define MONO(rd,gn,bl) (((rd)*11 + (gn)*16 + (bl)*5) >> 5)  /*.33R+ .5G+ .17B*/
  47.  
  48. /* RANGE forces a to be in the range b..c (inclusive) */
  49. #define RANGE(a,b,c) { if (a<b) a=b;  if (a>c) a=c; }
  50.  
  51. /* BIT_IS_ON determines whether a bit is set in a byte */
  52. #define BIT_IS_ON(p,key) ((p) & (key))
  53.  
  54. /* TURN_BIT_ON sets a bit in a byte.  Remember not to use a ";" */
  55. #define TURN_BIT_ON(p,key) {if(!BIT_IS_ON((p),(key))) p += (key);}
  56.  
  57. /* TURN_BIT_OFF zeros a bit in a byte.  Remember not to use a ";" */
  58. #define TURN_BIT_OFF(p,key) {if(BIT_IS_ON((p),(key))) p -= (key);}
  59.  
  60. /* DISTANCE returns the distance between two 3-d points */
  61. #define DISTANCE(x1,y1,z1,x2,y2,z2) sqrt((double)(((x2)-(x1))*((x2)-(x1))+((y2)-(y1))*((y2)-(y1))+((z2)-(z1))*((z2)-(z1))))
  62.  
  63. #define For_all_bits for(i = 0; i < NUM_OF_BITS; i++)
  64. #define For_rgb for(i = 0; i < NUM_CPLANES; i++)
  65. #define For_all_windows for(i = 0; i < NUM_WINS; i++)
  66. #define For_all_aspects for(i = 0; i < NUM_ASPECTS; i++)
  67. #define For_all_view for(i = 0; i <= WIN_VZ; i++)
  68. #define For_all_view_handles for(i = 0; i <= NUM_VIEW_HANDLES; i++)
  69. #define For_all_masks for(i = 0; i < MAX_MASKS; i++)
  70. #define For_all_loaded_masks for(i = 0; i < segal.num_m; i++)
  71. #define For_all_directions for(i = 0; i < NUM_DIRECTIONS; i++)
  72. #define For_all_bridge_strengths for(i = 0; i < NUM_BRIDGE_DIRS; i++)
  73.  
  74. #define vprint fprintf(stderr,
  75. #define vprint_if if(verbose)fprintf(stderr,
  76.  
  77. /***** Constants *****/
  78. #define UNDEFINED    -1
  79. #define UNDEFINED_BYTE    255
  80. #define LOGIC unsigned char
  81. #define FALSE    0
  82. #define TRUE    1
  83.  
  84. /* Running things in the background */
  85. #define INTERVAL_SEC    0
  86. #define INTERVAL_uSEC   30000   /* 30 microseconds */
  87.  
  88. /* job types */
  89. #define MAX_JOBS    20    /* Max # bg jobs allowed in queue at once */
  90. #define JOB_QUIT    -1
  91. #define JOB_LOAD_IMAGE    0
  92. #define JOB_LOAD_MASK    1
  93. #define JOB_SAVE_IMAGE    2
  94. #define JOB_SAVE_MASK    3
  95.  
  96. /* semaphore flags */
  97. #define UNLOCKED    0
  98. #define LOCKED        1
  99.  
  100. /* mask colors */
  101. #define PALSIZE        10
  102. #define RED        0    /* stack[0] will be red */
  103. #define BROWN        1
  104. #define ORANGE        2
  105. #define YELLOW        3
  106. #define GREEN        4
  107. #define TURQUOISE    5
  108. #define BLUE        6
  109. #define PURPLE        7    /* stack[7] will be purple */
  110. #define CBLACK        8    /* actual color black */
  111. #define CWHITE        9    /* actual color white */
  112.  
  113. /* segal internal mask colors/operations */
  114. #define BLACK    0
  115. #define WHITE    1
  116. #define INVERT    2
  117.  
  118. /* crop stuff */
  119. #define CROP_RESTORE    0
  120. #define CROP_FILL    1
  121.  
  122. /* maximums */
  123. #define NUM_OF_BITS        8
  124. #define MAX_MASKS        7    /* 7 masks + 1 region of interest */
  125. #define MAX_ZOOM_MAG        6
  126. #define NUM_BRUSH_SIZES        7
  127. #define MAX_BRUSH_SIZE        20
  128. #define NUM_GRAY        64
  129. #define NUM_COLORS        100    /* 2*NUM_GRAY + 100 = 228 ... */
  130. #define DELTA            (float) (NUM_GRAY - 1) / (256.)
  131. #define NUM_PTS_PER_ALLOC    (segal.r * segal.c / 4)
  132. #define NUM_BRIDGE_DIRS        8
  133. #define NUM_VIEW_HANDLES    4
  134.  
  135. /* quantizing */
  136. #define Q_BEST        0
  137. #define Q_MEDIUM    1
  138. #define Q_WORST        2
  139.  
  140. /* color image buffers */
  141. #define NUM_CPLANES    3    /* RGB */
  142. #define GRAY        0    /* grayscale image */
  143. #define RP        0
  144. #define GP        1
  145. #define BP        2
  146. #define VAL_RGB        3    /* MONO(r,g,b) */
  147.  
  148. /* windows */
  149. #define NUM_WINS    5
  150. #define WIN_VX        0
  151. #define WIN_VY        1
  152. #define WIN_VZ        2
  153. #define WIN_PAINT    3
  154. #define WIN_REF        4
  155.  
  156. /* aspects */
  157. #define NUM_ASPECTS    3
  158. #define ASPECT_X    0
  159. #define ASPECT_Y    1
  160. #define ASPECT_Z    2
  161.  
  162. /* misc. buffer types */
  163. #define BUF_PTS        5
  164. #define BUF_UNDO    6
  165.  
  166. /* bit mask key indices */
  167. #define MASK_UNDO_X    0
  168. #define MASK_UNDO_Y    1
  169. #define MASK_UNDO_Z    2
  170. #define MASK_CLIP_X    3
  171. #define MASK_CLIP_Y    4
  172. #define MASK_CLIP_Z    5
  173. #define MASK_UNDO_3d    6
  174. #define MASK_ROI_3d    7
  175.  
  176. /* mask types */
  177. #define MASK_EDIT    0
  178. #define MASK_NO_APPLY    1
  179. #define MASK_INCLUSIVE    2
  180. #define MASK_EXCLUSIVE    3
  181.  
  182. /* mask colors */
  183. #define MASK_RED    0
  184. #define MASK_GREEN    1
  185. #define MASK_BLUE    2
  186. #define MASK_YELLOW    3
  187.  
  188. /* 3d directions */
  189. #define NUM_DIRECTIONS    6
  190. #define DIR_N        0
  191. #define DIR_S        1
  192. #define DIR_E        2
  193. #define DIR_W        3
  194. #define DIR_U        4
  195. #define DIR_D        5
  196.  
  197. /* growth directions */
  198. #define DIR_GROW    0
  199. #define DIR_SHRINK    1
  200.  
  201. /* growth extents */
  202. #define GROW_FRAME    0
  203. #define GROW_BEG_TO_END    1
  204. #define GROW_ALL    2
  205.  
  206. /* seed points source */
  207. #define SEED_EDIT    0
  208. #define SEED_PTS    1
  209. #define SEED_ROI    2
  210.  
  211. /* roi's */
  212. #define R2d        0
  213. #define R3d        1
  214. #define R2d_WHOLE    2
  215. #define R2d_CROP    3
  216. #define R2d_PT_LIST    4
  217. #define R3d_WHOLE    5
  218. #define R3d_CUBE    6
  219. #define R3d_PT_LIST    7
  220.  
  221. /* order of apply */
  222. #define ORDER_EI    0
  223. #define ORDER_IE    1
  224.  
  225. /* display options */
  226. #define DISP_IMAGE    0
  227. #define DISP_MASK    1
  228. #define DISP_ORIG    2
  229. #define DISP_XY        3
  230.  
  231. /* growth display options */
  232. #define DISP_WHEN_DONE    0
  233. #define DISP_GROW    1
  234. #define DISP_ALL    2
  235.  
  236. /* modes */
  237. #define MODE_SEGMENT    0
  238. #define MODE_REGISTER    1
  239.  
  240. /* brush modes */
  241. #define BRUSH_IMAGE    0    /* Painting the Image with an airbrush */
  242. #define BRUSH_MASK    1    /* Painting the mask */
  243. #define BRUSH_PTS    2    /* Painting the point list */
  244.  
  245. /* brush shapes */
  246. #define BRUSH_SQUARE    0
  247. #define BRUSH_ROUND    1
  248.  
  249. /* mask brush affects */
  250. #define MASK_ERASE    0
  251. #define MASK_PAINT    1
  252.  
  253. /* threshold effects */
  254. #define THRESH_OVERWRITE    0
  255. #define THRESH_ADD_TO        1
  256. #define THRESH_REMOVE_FROM    2
  257.  
  258. /***** Structures *****/
  259. typedef struct {
  260.     int    x, y, z;    /* current x, y, z */
  261.     int    r1, c1, f1, r2, c2, f2; /* used to determine r, c, f */
  262.     int    r, c, f;    /* rows, columns, frames of what is loaded */
  263.     LOGIC    color;        /* load image as a color or gray image? */
  264.     LOGIC    r3d;        /* 3d = TRUE, 2d = FALSE */
  265.     int    roi;        /* region of interest */
  266.     LOGIC    disp_image;    /* display image? */
  267.     LOGIC    disp_mask;    /* display mask? */
  268.     LOGIC    disp_pts;    /* display points? */
  269.     LOGIC    disp_xy;    /* display X & Y views? */
  270.     int    mode;        /* segmentation or registration */
  271.     int    bg_i;        /* background job index */
  272.     int    num_m;        /* number of masks loaded (7 max) */
  273.     int    e_m;        /* current edit mask */
  274.     int    new_m;        /* new mask */
  275.     }    SEGAL_INFO;
  276.  
  277. typedef struct {
  278.     int    sel_m;        /* selected mask in the log */
  279.     LOGIC    apply_log;    /* Auto apply log when painting? */
  280.     int    apply_order;    /* exclusive-inclusive, or vice-versa? */
  281.     }    MLOG_INFO;
  282.  
  283. typedef struct {
  284.     int    job;        /* load, save, quit, etc. */
  285.     int    arg;        /* argument for this job */
  286.     }    QUEUE_EL;
  287.  
  288. typedef struct {
  289.     int    semaphore;    /* locked/unlocked: 1 bg job at a time */
  290.     int    width, height;    /* timer window */
  291.     XID    xid;        /* parent window of bg job */
  292.     char    message[MAXPATHLEN];
  293.                 /* job description */
  294.     int    qfront, qrear;    /* for the scheduling queue */
  295.     QUEUE_EL queue[MAX_JOBS];
  296.                 /* scheduling queue */
  297.     }    TIMER_INFO;
  298.  
  299. typedef struct {
  300.     LOGIC    loaded;        /* an image was successfuly loaded */
  301.     Image    hd;        /* structure for using Jin's ccl */
  302.     int    r, c, f;    /* of stored image file */
  303.     int    frame_size;    /* of stored image file */
  304.     char    dname[MAXPATHLEN];
  305.     char    fname[MAXPATHLEN];
  306.     FILE    *fp;        /* fp for the image */
  307.     LOGIC    color;        /* color image? */
  308.     LOGIC    changed_image;
  309.     LOGIC    changed_frame;
  310.     byte    *undo[NUM_ASPECTS][NUM_CPLANES];
  311.     byte    *orig[NUM_ASPECTS][NUM_CPLANES];
  312.     }    IMAGE_INFO;
  313.  
  314. typedef struct {
  315.     LOGIC    loaded;        /* mask was successfuly loaded */
  316.     Image    hd;
  317.     int    f;
  318.     char    dname[MAXPATHLEN];
  319.     char    fname[MAXPATHLEN];
  320.     LOGIC   changed_mask;
  321.     LOGIC   changed_frame;
  322.     int    mask_type;    /* edit, inclusive, exclusize, etc. */
  323.     int    mask_hue;    /* green, blue, red, etc. */
  324.     int    bit_key;    /* which mask bit */
  325.     }    MASK_INFO;
  326.  
  327. typedef struct {
  328.     int    zoom_mag;    /* magnification to zoom the window */
  329.     int    aspect;        /* variable for paint and reference windows */
  330.     int    f;        /* current frame */
  331.     int    img_r, img_c, img_size;
  332.     int    canv_x, canv_y, canv_w, canv_h;
  333.     LOGIC    repaint;
  334.     XID    xid;
  335.     Scrollbar h_sbar, v_sbar; /* horizontal & vertical scrollbars */
  336.     byte    ***i_data;    /* color planes X rows X cols of one slice */
  337.     byte    **m_data;    /* one slice of the bit masks */
  338.     byte    **z_data;    /* zoom area containing replicated xcolors */
  339.     XImage    *ximg;
  340.     }    WIN_INFO;
  341.  
  342. typedef struct {
  343.     int    x, y;
  344.     }    HANDLE_INFO;
  345.  
  346. typedef struct {
  347.     int    win_id;
  348.     int    x1, y1, f1, x2, y2, f2;
  349.     HANDLE_INFO handle[NUM_ASPECTS][NUM_VIEW_HANDLES];
  350.     }    CROP_INFO;
  351.  
  352. typedef struct {
  353.     unsigned short  x, y, f;
  354.     }       POINT_TYPE;
  355.  
  356. typedef struct {
  357.     byte    r, g, b;    /* color components */
  358.     }    COLOR_TYPE;
  359.  
  360. typedef struct {
  361.     byte    r, g, b;    /* color components */
  362.     u_int    count;        /* count for histogram */
  363.     byte    col_i;        /* index into the allocated color[] */
  364.     }    RGB_HISTO;
  365.  
  366. typedef struct {
  367.     int    quality;
  368.     int    win;
  369.     int    num_from_histo;
  370.     int    auto_r, auto_g, auto_b;
  371.     int    max_samples;    /* the more the better quality but slower */
  372.     int    min_rgb_dist;    /* keeps alloc'd colors apart in rgb-space */
  373.     }    QUANT_INFO;
  374.  
  375. typedef struct {
  376.     QUANT_INFO quant;
  377.     int    num_histo;
  378.     RGB_HISTO *histo;
  379.     int    map_r, map_g, map_b;
  380.     byte    ***map;
  381.     }    RGB_INFO;
  382.  
  383. typedef struct {
  384.     /* x, y, f, etc. are in the Z aspect */
  385.     int    x1, y1, f1;    /* left upper below corner */
  386.     int    x2, y2, f2;    /* right lower above corner */
  387.     int    beg_frame;
  388.     int    end_frame;
  389.     }    REGION_INFO;
  390.  
  391. typedef struct {
  392.     int    min, max;
  393.     int    roi;
  394.     int    plane;
  395.     int    mask_effect;
  396.     int    image_effect;
  397.     int    degree;
  398.     XID    xid;
  399.     }    THRESH_INFO;
  400.  
  401. typedef struct {
  402.     int    offset;
  403.     int    current;
  404.     int    upper;
  405.     int    lower;
  406.     }    FRAME_INFO;
  407.  
  408. typedef struct {
  409.     int    mode;
  410.     int    image_affect;
  411.     int    degree;
  412.     int    mask_affect;
  413.     int    shape;
  414.     int    size;
  415.     }    BRUSH_INFO;
  416.  
  417. typedef struct {
  418.     int    swin;
  419.     int    direction;
  420.     LOGIC    apply_thresholds;
  421.     int    threshold_min;
  422.     int    threshold_max;
  423.     LOGIC    apply_gradient;
  424.     int    gradient_rad;
  425.     int    gradient_min;
  426.     int    gradient_max;
  427.     LOGIC    apply_bridge;
  428.     int    bridge_dist;
  429.     int    bridge_min;
  430.     int    bridge_max;
  431.     int    extent;
  432.     int    seed_pt_src;
  433.     LOGIC    overwrite;
  434.     int    disp_growth;
  435.     LOGIC    interractive;
  436.     struct itimerval itimer;
  437.     int    speed;        /* number of points per interval */
  438.     LOGIC    stack_empty;
  439.     }    GROW_INFO;
  440.  
  441. typedef struct {
  442.     int num_pts;
  443.     int num_pts_per_alloc;
  444.     int max_pts;
  445.     POINT_TYPE *pts;
  446.     }    STACK_INFO;
  447.  
  448. typedef struct curs {
  449.     Xv_Cursor paint;    /* round cursors */
  450.     Xv_Cursor erase;
  451.     Xv_Cursor sq_paint;    /* square cursors */
  452.     Xv_Cursor sq_erase;
  453.     u_char **paint_mask_matrix; /* gives shape to the round cursors */
  454.     int size;
  455.     int radius;
  456.     int corner;
  457.     }    SEGAL_CURSOR;
  458.  
  459.  
  460. /***** User Interface Objects *****/
  461. extern file_pop_load_image_objects     *File_pop_load_image;
  462. extern file_pop_load_mask_objects      *File_pop_load_mask;
  463. extern file_pop_new_mask_objects       *File_pop_new_mask;
  464. extern file_pop_save_as_objects        *File_pop_save_as;
  465. extern file_pop_save_image_objects     *File_pop_save_image;
  466. extern filter_pop_filter_objects       *Filter_pop_filter;
  467. extern image_reg_pop_image_reg_objects *Image_reg_pop_image_reg;
  468. extern image_reg_pop_ref_frame_objects *Image_reg_pop_ref_frame;
  469. extern list_pop_list_objects           *List_pop_list;
  470. extern mask_grow_pop_mask_grow_objects *Mask_grow_pop_mask_grow;
  471. extern mask_grow_pop_options_objects   *Mask_grow_pop_options;
  472. extern mask_log_pop_mask_log_objects   *Mask_log_pop_mask_log;
  473. extern mask_log_pop_options_objects    *Mask_log_pop_options;
  474. extern paint_win_paint_objects         *Paint_win_paint;
  475. extern preferences_pop_preferences_display_objects
  476.                     *Preferences_pop_preferences_display;
  477. extern threshold_pop_threshold_objects *Threshold_pop_threshold;
  478. extern view_pop_timer_objects           *View_pop_timer;
  479. extern view_win_objects                *View_win;
  480.  
  481.  
  482. /***** Global Variables *****/
  483. extern BRUSH_INFO    brush;
  484. extern CROP_INFO    crop;
  485. extern FRAME_INFO    frame;
  486. extern GROW_INFO    grow;
  487. extern IMAGE_INFO    img;
  488. extern MASK_INFO    m[MAX_MASKS + 1];
  489. extern MLOG_INFO    mlog;
  490. extern REGION_INFO    region;
  491. extern RGB_INFO        rgb;
  492. extern SEGAL_CURSOR    my_cursor[NUM_BRUSH_SIZES][MAX_ZOOM_MAG];
  493. extern SEGAL_INFO    segal;
  494. extern THRESH_INFO    threshold;
  495. extern TIMER_INFO    timer;
  496. extern WIN_INFO        win[NUM_WINS];
  497.  
  498. /* Running things in the background */
  499. extern struct itimerval itimer;
  500.  
  501. /* HIPS header stuff */
  502. extern int ac;
  503. extern char **av;
  504.  
  505. /* command line args */
  506. extern char    *in_image, *in_mask, *in_list;
  507. extern int    overlay_hue;
  508.  
  509. /* colors */
  510. extern int num_rgb;        /* number of colors in rgb_histo */
  511.  
  512. /* 3d buffers */
  513. extern u_char    ***cbuf[NUM_CPLANES]; /* space for a 3d color image */
  514. extern u_char    ***ibuf;    /* space for a 3d gray image */
  515. extern u_char    ***mbuf;    /* space for 7 bit-masks and a 3d region */
  516.  
  517. /* 1d buffers */
  518. extern u_char    *bm;        /* bit masks ... */
  519. extern byte    bm_key[NUM_OF_BITS];
  520.  
  521. /***** X Windows *****/
  522. /* window stuff shared by all parts - DEFINED in segal.c */
  523. extern Display *display;
  524. extern u_long *colors;
  525. extern GC gc;
  526. extern int screen;
  527. extern Xv_cmsdata cms_data;
  528. extern Visual *winv;
  529. extern XVisualInfo *winv_info;
  530. extern Window mainw;
  531.  
  532. extern u_char *image_map;
  533. extern int im_size;
  534.  
  535. /* cmap.c */
  536. extern u_char gray_lut[256];    /* just image */
  537. extern u_char blend_lut[256];    /* mask-image blend */
  538. extern u_char pt_lut[256];    /* point list */
  539.  
  540. /* cursor used everywhere */
  541. extern Cursor watch_cursor;
  542.  
  543. /* preferences */
  544. extern int    verbose;
  545.  
  546. /***** Global Routines *****/
  547. void set_watch_cursor();
  548. void unset_watch_cursor();
  549.